//
//  ViewController.m
//  9-20
//
//  Created by mac on 16/9/20.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"
#import "TableViewController.h"
#import "ChatTableViewController.h"
#import "ListDataViewController.h"
#import "LoginData.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UITextField *text1;
@property (strong, nonatomic) IBOutlet UITextField *text2;

@end

@implementation ViewController
- (IBAction)onClick:(UIButton *)sender
{
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://xl18z.cn/login.php?name=%@&password=%@",self.text1.text,self.text2.text]];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *task = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
       
        NSString *text = [[NSString alloc] initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
        
        NSLog(@"%@",text);
        
          NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        LoginData *login = [[LoginData alloc] initWithDictionary:dict];
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if(login.errcode == 0)
            {
               
               ListDataViewController *viewcont = [self.storyboard instantiateViewControllerWithIdentifier:@"ListData"];
                [self.navigationController pushViewController:viewcont animated:YES];
            }
            else
            {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号或密码错误" preferredStyle:UIAlertControllerStyleAlert];
                [alert addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil]];
                [self presentViewController:alert animated:YES completion:nil];
                NSLog(@"%@",login.errinfo);
            }
        });
    }];
    
    [task resume];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.text1.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.text2.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.text2.secureTextEntry = YES;
    self.text1.text = @"test";
    self.text2.text = @"123";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
